package de.fhkl.android.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;

import org.xmlpull.v1.XmlSerializer;

import android.content.Context;
import android.util.Xml;

public class XMLWriter {
	private String writeXml() {
		XmlSerializer serializer = Xml.newSerializer();
		StringWriter writer = new StringWriter();
		try {
			serializer.setOutput(writer);

			// <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
			serializer.startDocument("UTF-8", true);

			// <blog number="1">
			serializer.startTag("", "blog");
			serializer.attribute("", "number", String.valueOf(1));

			// <message data="2009-09-23">
			serializer.startTag("", "message");
			serializer.attribute("", "date", "2009-09-23");

			// <title>Android XML</title>
			serializer.startTag("", "title");
			serializer.text("Android XML");
			serializer.endTag("", "title");

			// <url>http://www.moandroid.com/?p=508</url>
			serializer.startTag("", "url");
			serializer.text("http://www.moandroid.com/?p=508");
			serializer.endTag("", "url");

			// </message>
			serializer.endTag("", "message");
			// </blog>
			serializer.endTag("", "blog");
			serializer.endDocument();
			return writer.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}
	/**
	 * 
	 * @param filename file name with path
	 * @param content file content as String
	 * @param ctx context from android view
	 * @return true = write successful
	 */
	public boolean Write(String filename, String content, Context ctx) {
		try {
			OutputStream os = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
			OutputStreamWriter osw = new OutputStreamWriter(os);
			osw.write(content);
			osw.close();
			os.close();
		} catch (FileNotFoundException e) {
			return false;
		} catch (IOException e) {
			return false;
		}
		return true;
	}

	public static void writeFile(String filePath, String fileName,
			String content) {

	}
}
